<?php
App::uses('AppController', 'Controller');
/**
 * Admins Controller
 *
 * @property Admin $Admin
 */
class AdminsController extends AppController {

  public function beforeFilter(){
    parent::beforeFilter();
    $this->Auth->allow('*');
    $this->loadModel('User');
    $group = $this->User->Group;
    $group->id = $this->Auth->user('group_id');
    if(!$this->Acl->check($group, 'controllers/Admins')){
      $this->redirect($this->Auth->redirect());
    }
    $this->set('title_for_layout', '相册管理后台'); 
  }
  public function index() {
    $group = $this->User->Group;
    $group->id = $this->Auth->user('group_id');
    $this->set('flag', $this->Acl->check($group, 'controllers/Admins', 'read'));
  }

  public function users() {
    $this->User->recursive = 0;
    $this->set('users', $this->paginate('User'));
  }

  public function addUser() {
    if ($this->request->is('post')) {
      $this->User->create();
      if ($this->User->save($this->request->data)) {
        $this->Session->setFlash(__('The user has been saved'));
        $this->redirect(array('action' => 'index'));
      } else {
        $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
      }
    } else {
      $groups = $this->User->Group->find('list');
      $this->set(compact('groups'));
    }
  }

  public function groups() {
    $this->loadModel('Group');
    $this->set('groups', $this->paginate('Group'));
  }

  public function addGroup() {
    $this->loadModel('Group');
    if($this->request->is('post')) {
      if($this->Group->save($this->data)) {
        $this->setFlash('Save Successful');
      } else {
        $this->setFlash('Save failer, try again');
      }
    } 
  }

  public function categories() {
    $this->loadModel('Category');
		$this->Category->recursive = 0;
		$this->set('categories', $this->paginate('Category'));
  }

  public function addCategory() {
    $this->loadModel('Category');
		if ($this->request->is('post')) {
			$this->Category->create();
			if ($this->Category->save($this->request->data)) {
				$this->Session->setFlash(__('The category has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The category could not be saved. Please, try again.'));
			}
    } else {
      $parent[0] = '顶级目录';
      $categories = $this->Category->generateTreeList(null, null, null, '--');
      foreach($categories as $key=>$value) {
        $parent[$key] = $value;
      }
      $this->set('parents', $parent);
    }
  }

}
